package cn.miju.admin.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.miju.admin.framework.model.BasePage;
import cn.miju.admin.system.service.OperLogService;
import cn.miju.admin.system.vo.OperLogPageInVO;
import cn.miju.admin.system.vo.OperLogPageOutVO;
import cn.miju.database.entity.SysOperLog;
import cn.miju.database.mapper.SysOperLogMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * <pre>
 * 操作日志
 * </pre>
 *
 * @author zhuming
 * @version 1.0.0
 * @since 2023-06-21 14:20
 */
@Service
@RequiredArgsConstructor
public class OperLogServiceImpl implements OperLogService {
    private final SysOperLogMapper sysOperLogMapper;

    /**
     * 分页查询操作日志
     * @param inVO {@link OperLogPageInVO}
     * @return {@link Page<OperLogPageOutVO>}
     */
    @Override
    public Page<OperLogPageOutVO> getPage(OperLogPageInVO inVO) {
        sysOperLogMapper.delete(Wrappers.<SysOperLog>lambdaQuery().lt(SysOperLog::getOperDate, LocalDate.now().minusDays(7)));
        Page<SysOperLog> operLogPage = sysOperLogMapper.selectPage(new Page<>(inVO.getPage(), inVO.getPageSize()),
                Wrappers.<SysOperLog>lambdaQuery().eq(SysOperLog::getUserId, inVO.getLoginUser().getId()));
        Page<OperLogPageOutVO> page = new Page<>();
        BeanUtil.copyProperties(operLogPage, page);
        if (CollUtil.isNotEmpty(operLogPage.getRecords())) {
            page.setRecords(operLogPage.getRecords().stream().
                    map(operLog -> BeanUtil.copyProperties(operLog, OperLogPageOutVO.class)).collect(Collectors.toList()));
        }
        return page;
    }
}
